using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Data;
using System.Collections.ObjectModel;
using Microsoft.Practices.CompositeWeb;
using Microsoft.Practices.ObjectBuilder;
using WCSFContrib.ECommerceCatalog.BusinessEntities;
using WCSFContrib.ECommerceCatalog.CatalogData;
using WCSFContrib.ECommerceCatalog.Product.Interfaces.Services;

namespace WCSFContrib.ECommerceCatalog.Product.Services
{
    public class ItemsService : IItemsService
    {
        private CatalogDataSet _catalogDataSet;
        ICategoriesService _categoriesService;

        public ItemsService([ServiceDependency] ICategoriesService categoriesService)
            : this(CatalogRepository.Instance, categoriesService)
        {
        }

        public ItemsService(CatalogDataSet ds, ICategoriesService categoriesService)
        {
            _catalogDataSet = ds;
            _categoriesService = categoriesService;
        }

        public ICollection<Item> AllItems
        {
            get
            {
                ICollection<Item> items = new List<Item>();
                foreach (CatalogDataSet.ItemRow row in _catalogDataSet.Item.Rows)
                {
                    items.Add(TranslateFromItemRowToItemEntity(row));
                }
                return items;
            }
        }

        public Item GetItemById(int itemId)
        {
            CatalogDataSet.ItemRow itemRow = _catalogDataSet.Item.FindByItemId(itemId);
            Item item;

            if (itemRow == null)
                return null;

            item = TranslateFromItemRowToItemEntity(itemRow);

            return item;
        }

        public ICollection<Item> GetItemsByCategory(int categoryId)
        {
            DataRow[] rows = _catalogDataSet.Item.Select(String.Format(CultureInfo.CurrentCulture, "Category='{0}'", categoryId));
            IList<Item> items = new List<Item>(rows.Length);

            foreach (CatalogDataSet.ItemRow itemsRow in rows)
            {
                items.Add(TranslateFromItemRowToItemEntity(itemsRow));
            }

            return items;
        }

        public void UpdateItem(Item item)
        {
            if (item == null)
                return;

            CatalogDataSet.ItemRow itemRow = _catalogDataSet.Item.FindByItemId(item.ItemId);

            if (itemRow == null)
                return;

            itemRow.Name = item.Name;
            itemRow.Description = item.Description;
            itemRow.Image = item.Image;
            itemRow.Stock = item.Stock;
            itemRow.Price = item.Price;

            if (item.Category == null)
                return;
            
            itemRow.Category = item.Category.CategoryId;
        }

        public ICollection<Item> SearchItems(string searchText)
        {
            string query = string.Format(CultureInfo.InvariantCulture, "Name like '*{0}*' OR Description like '*{0}*'", searchText);
            CatalogDataSet.ItemRow[] itemsRows = _catalogDataSet.Item.Select(query) as CatalogDataSet.ItemRow[];
            ICollection<Item> itemList = new Collection<Item>();
            foreach (CatalogDataSet.ItemRow row in itemsRows)
            {
                Item item = TranslateFromItemRowToItemEntity(row);
                itemList.Add(item);
            }

            return itemList;
        }

        public ICollection<Item> SearchItems(string searchText, string categoryId)
        {
            string query = string.Format(CultureInfo.InvariantCulture, "(Name like '*{0}*' OR Description like '*{0}*')", searchText);

            if (categoryId.Length > 0)
            {
                query += string.Format(CultureInfo.InvariantCulture, " AND (Category = {0})", categoryId);
            }

            CatalogDataSet.ItemRow[] itemsRows = _catalogDataSet.Item.Select(query) as CatalogDataSet.ItemRow[];
            ICollection<Item> itemList = new Collection<Item>();
            foreach (CatalogDataSet.ItemRow row in itemsRows)
            {
                Item item = TranslateFromItemRowToItemEntity(row);
                itemList.Add(item);
            }

            return itemList;
        }

        private Item TranslateFromItemRowToItemEntity(CatalogDataSet.ItemRow itemsRow)
        {
            Item item = new Item();

            item.ItemId = itemsRow.ItemId;
            item.Name = itemsRow.IsNameNull() ? null : itemsRow.Name;
            item.Description = itemsRow.IsDescriptionNull() ? null : itemsRow.Description;
            item.Image = itemsRow.IsImageNull() ? null : itemsRow.Image;
            item.Stock = itemsRow.Stock;
            item.Price = itemsRow.Price;
            item.Category = _categoriesService.GetCategoryById(itemsRow.Category);

            return item;
        }
    }
}
